package scau.erps.view.business.staff;

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.RowFilter;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

import scau.erps.app.Erps;
import scau.erps.entity.Team;
import scau.erps.entity.TeamOrder;
import scau.erps.service.TeamOrderService;
import scau.erps.service.impl.TeamOrderServiceImpl;


/**
 * 客户界面类
 * @author DCJ
 *
 */
public class OrderPanel extends BusinessPanelTemplate{
	private static final long serialVersionUID = 1L;
	private OrderTableModel dataModel;
	public OrderPanel(){
		super();
		this.setComboItemsValue();
		this.operationBtn1.setText("交货");	
		this.approveBtn.setVisible(false);
		this.operationBtn2.setVisible(false);
		this.dataModel = getOrderTableModel();
		this.table.setModel(dataModel);
	}
	private OrderTableModel getOrderTableModel() {
		Team team = Erps.getCurrentTeam();
		TeamOrderService orderService = Erps.getIocInstance().get(
				TeamOrderServiceImpl.class);
		List<TeamOrder> list = null;
		if (team == null) { // 工作人员登陆
			list = orderService.getAllOrders();
		} else {
			list = orderService.getOrders(team.getId(), null, null, null);
		}
		dataModel = new OrderTableModel(list);
		return dataModel;
	}
	@Override
	public void setComboItemsValue() {
		String[] orderType = new String[]{"订单区域","本地","区域","国内","亚洲","国际"};
		String[] orderStatus = new String[]{"订单状态","未交货","已交货","延迟交货"};
		this.addItems(businessType, orderType);
		this.addItems(businessStatus,orderStatus);
		
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == searchBtn){
			String team = null;
			if (teamCombo.getSelectedIndex() != 0) {
				team = (String) teamCombo.getSelectedItem();
			}			
			String type = null;
			if (businessType.getSelectedIndex() != 0) {
				type = (String) businessType.getSelectedItem();
			}
			String status = null;
			if (businessStatus.getSelectedIndex() != 0) {
				status = (String) businessStatus.getSelectedItem();
			}
			final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(dataModel);
			List<RowFilter<Object, Object>> filters = new ArrayList<RowFilter<Object, Object>>(2);
			if(team !=null){
				filters.add(RowFilter.regexFilter(team));
			}
			if (type != null) {
				filters.add(RowFilter.regexFilter(type));
			}
			if (status !=null){
				filters.add(RowFilter.regexFilter(status));
			}				
			sorter.setRowFilter(RowFilter.andFilter(filters));			
			this.table.setRowSorter(sorter);
			this.table.setModel(dataModel);
		}
		else if(e.getSource()==approveBtn){
			
		}
		else if(e.getSource()==operationBtn1){
			
		}
		
	}
}

/**
 * 表格数据模型
 * 
 * @author DCJ
 *
 */
class OrderTableModel extends AbstractTableModel {
	private static final long serialVersionUID = 1L;
	private String[] title = { "序号", "小组", "年份", "产品", "市场", "数量","金额", "交货期","收款期","ISO","状态","罚金","成本"};
	private Object[][] data = null;

	public OrderTableModel(List<TeamOrder> list) {
		data = new Object[list.size()][title.length];
		Iterator<TeamOrder> iter = list.iterator();
		for (int i = 0; i < list.size(); i++) {
			TeamOrder teamOrder = iter.next();
			data[i][0] = teamOrder.getId();
			data[i][1] = "第"+teamOrder.getTeam_Id()+"组";
			data[i][2] = teamOrder.getYear();
			data[i][3] = teamOrder.getProduct();
			data[i][4] = teamOrder.getMarket();
			data[i][5] = teamOrder.getQuantity();
			data[i][6] = teamOrder.getTotal();
			data[i][7] = teamOrder.getDeliver();
			data[i][8] = teamOrder.getReceivable();
			data[i][9] = teamOrder.getIso();
			data[i][10] = teamOrder.getStatus();
			data[i][11] = teamOrder.getPenalty();
			data[i][12] = teamOrder.getCost();
		}
	}

	@Override
	public int getRowCount() {
		return this.data.length; // 行数
	}

	@Override
	public int getColumnCount() {
		return this.title.length; // 列数
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		return data[rowIndex][columnIndex]; // 取表格单元格值
	}

	public String getColumnName(int columnIndex) {
		return title[columnIndex]; // 显示标题
	}
}